package yxy.game.pm2.module.friend;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.friendsdate;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.HashSet;

public class FriendModule extends PlayerModule {
	// bean===========================================================================

	private DateTime lastTime;// 刷新时间
	private HashSet<String> friend = new HashSet<>();// 好友列表
	private HashSet<String> apply = new HashSet<>();// 申请列表
	private HashSet<String> given = new HashSet<>();// 赠送过的好友
	private HashSet<String> giftn = new HashSet<>();// 送礼的好友

	private HashSet<String> black = new HashSet<>();// 好友列表

	public DateTime getLastTime() {
		return lastTime;
	}

	public void setLastTime(DateTime lastTime) {
		this.lastTime = lastTime;
	}

	public HashSet<String> getGiven() {
		return given;
	}

	public void setGiven(HashSet<String> given) {
		this.given = given;
	}

	public HashSet<String> getFriend() {
		return friend;
	}

	public void setFriend(HashSet<String> friend) {
		this.friend = friend;
	}

	public HashSet<String> getApply() {
		return apply;
	}

	public void setApply(HashSet<String> apply) {
		this.apply = apply;
	}

	public HashSet<String> getBlack() {
		return black;
	}

	public void setBlack(HashSet<String> black) {
		this.black = black;
	}

	public void setGiftn(HashSet<String> giftn) {
		this.giftn = giftn;
	}

	public HashSet<String> getGiftn() {
		return giftn;
	}

	// func===========================================================================

	public friendsdate data() {
		return DataCenter.getData(1, friendsdate.class);
	}

	/** 刷新每日的状态 */
	public void update() {
		DateTime now = DateTime.now();
		if (getLastTime() == null || !new LocalDate(now).equals(new LocalDate(getLastTime()))) {
			try {
				if (tryLock()) {
					setLastTime(now);
					given.clear();
					this.set();
				}
			} catch (Exception e) {
			} finally {
				unlock();
			}
		}
		check();
	}

	/** 空缺的好友数 */
	public int vacant() {
		int max = fomula.好友数上限.intValue();
		int size = getFriend().size();
		return max - size;
	}

	public void check() {
		new TipHandle().updateTip(getPlayerCode(), TipType.主城_好友_好友切页, getGiven().size() < data().getUplimit() && getGiven().size() < getFriend().size());
		new TipHandle().updateTip(getPlayerCode(), TipType.主城_好友_申请切页, getApply().size() > 0);
		new TipHandle().updateTip(getPlayerCode(), TipType.主城_好友_赠送切页, getGiftn().size() > 0);
	}

	// redis==========================================================================

	static public String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:FRIEND", playerCode);
	}

	public void set() {
		try {
			String key = key(getPlayerCode());
			setv(key, this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public FriendModule get(String playerCode) {
		try {
			FriendModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), FriendModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}
			// TODO 创建模块需要移植等级模块开启处理里
			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

//	static public FriendModule get1(String playerCode) {
//		try {
//			FriendModule module = getv(key(playerCode));
//			if (module == null) {
//				module = MySQLCacheDAO.getV(key(playerCode), FriendModule.class);
//				if(module != null){module.set();}//把数据同步到redis
//			}
//			// TODO 创建模块需要移植等级模块开启处理里
//			if (module == null) {
//				module = create(playerCode);
//			}
//			return module;
//		} catch (RedisException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

	static private FriendModule create(String playerCode) {
		FriendModule module = new FriendModule();
		module.setPlayerCode(playerCode);
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				FriendModule friendModule = getv(key(playerCode));
				if(friendModule != null) MySQLCacheDAO.setV(key, friendModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

	public static void main(String[] args) {
		String json = "{\"apply\":Set[],\"black\":Set[],\"friend\":Set[\"6mlxt3\"],\"giftn\":Set[],\"given\":Set[\"6mlxt3\"],\"lastTime\":\"2022-01-31T00:09:18.286+08:00\",\"playerCode\":\"j4u4x6\",\"syncdb\":0}";
		FriendModule module = JSONObject.parseObject(json, FriendModule.class);
		HashSet<String> friends = module.getFriend();
		System.out.println(friends.size());
	}

}
